package service

import (
	"context"
	"fmt"
	"github.com/star-find-cloud/star-chat/email/domain"
	"github.com/star-find-cloud/star-chat/internal/mq"
	log "github.com/star-find-cloud/star-chat/pkg/logger"
)

type EmailServiceImpl struct {
	producer mq.ProducerSrv
}

func NewEmailService(producer mq.ProducerSrv) domain.EmailService {
	return &EmailServiceImpl{
		producer: producer,
	}
}

func (s *EmailServiceImpl) SendVerificationCode(ctx context.Context, email, name string) error {
	msg := map[string]string{"email": email, "name": name}
	key := fmt.Sprintf("%s_%s", email, name)

	err := s.producer.SendAsyncMsg(ctx, msg, "verification_code", key)
	if err != nil {
		log.AppLogger.Errorf("send verification code email message failed: %v", err)
		return err
	}

	log.AppLogger.Infof("send verification code email message success")
	return nil
}

func (s *EmailServiceImpl) SendHelloEmail(ctx context.Context, email, name string, id int64) error {
	msg := map[string]interface{}{"email": email, "name": name, "id": id}
	key := fmt.Sprintf("%s_%s_%d", email, name, id)

	err := s.producer.SendAsyncMsg(ctx, msg, "Hello", key)
	if err != nil {
		log.AppLogger.Errorf("send hello email message failed: %v", err)
		return err
	}

	log.AppLogger.Infof("send hello email message success")
	return nil
}
